#region Imported Namespaces
using System;
using Workflow.NET;
using WorFlowInterface = Workflow.NET.Engine.Interfaces;
using WorkFlowAction = Workflow.NET.ActionHandler;
using WorkFlowEngine = Workflow.NET.Engine;
using PHSRAG.Insight21.Policy;
using InsightUtility = PHSRAG.Utility;
using System.Data;
using System.Threading;
using System.Collections;
#endregion

namespace PHSRAG.Insight21.InsightWorkFlow
{
	#region Class CreateProtocolProcessWorkflowMap
	/// <summary>
	/// This is a Skelta workflow custom action class. This class is creating a Insight-humans and Skelta
	/// map in map ProtocolProcess_Workflow_Map table. IActionRun is Skelta interface to implement custom actions. 
	/// Actions.xml of particular application needs to be configured so that this action
	/// becomes accesible from Skelta Workflow Manager control under List of Actions -> Custom Actions. 
	/// This action takes a parameter which is defined in Actions.xml. The available values
	/// for the parameter are also defined in Actions.xml. They are Lock or Unlock.
	/// Depends on which paramter is set on the step on Workflow manager ProtocolProcessLock 
	/// or ProtocolProcessUnlock gets called.
	/// </summary>
	public class CreateProtocolProcessWorkflowMap:Workflow.NET.Engine.Interfaces.IActionRun 
	{		
		/// <summary>
		/// Default constructor
		/// </summary>
		public CreateProtocolProcessWorkflowMap()
		{
			
		}
		/// <summary>
		/// Custom action method used by Skelta Workflow engine to take certain action. 
		/// This method is calling HumansWorkflow object and creating a map between the Protocol Process and Skelta tables. 
		/// </summary>
		/// <param name="executionId">Workflow execution Id - a unique id used for each workflow item</param>
		/// <param name="executionDetailsId">Execution detail Id - Id used for execution detail table</param>
		/// <param name="currentContext">Context class which encapsulates all the information about the executing Workflow.</param>
		/// <param name="currentAction">Current Workflow action</param>
		/// <param name="inlinkOutput">Output in string format which has come from the step before this step</param>
		/// <param name="retry">Boolean retry variable</param>
		/// <param name="output">Output value which is used by Skelta Workflow manager to takce certain path in the process.</param>
		/// <returns>The result of an action execution. Either execution is complete or action is in sleep mode.  </returns>
		public  WorFlowInterface.ActionResult Run(int executionId,int executionDetailsId,WorkFlowEngine.Context currentContext,
		Action currentAction,string inlinkOutput,bool retry,out string output)
		{
            //System.Diagnostics.Debugger.Break();
			output=null;	
			Log logger = currentContext.log;
			try
			{
                object insightApplicationName = currentContext.Contents["insightApplicationName"];
                HumansWorkFlow.InsightApplicationName = (null == insightApplicationName) ? string.Empty : ((Content)insightApplicationName).Value.ToString();
                
                Thread.SetData(Thread.GetNamedDataSlot("Connections"), HumansWorkFlow.Connections);
				string workflowType = "INIT";
				if ( currentContext.Variables["workflowType"].Value != null )
				{
					if (currentContext.Variables["workflowType"].Value.ToString() == "RESUB" ) 
						workflowType = "RESUB";
				}
                //checks for submitter if remove and it is null will give "Object reference not set"
				int nSubmitterId= 0;
                if (currentContext.Variables["submitterUser"]!=null && currentContext.Variables["submitterUser"].Value != null &&
				currentContext.Variables["submitterUser"].Value.ToString().Trim() != "")
				{
					nSubmitterId = Int32.Parse(((ArrayList)(currentContext.Variables["submitterUser"].Value))[0].ToString());
				}
												
                InsightUtility.DataAccess.ExecuteNonQuery(HumansWorkFlow.InsightConnection, "CreateProtocolProcessWorkFlowMap",
					Utility.DataAccess.BuildSqlParameterArray(
							"@ProtocolProcessID",SqlDbType.Int,Int32.Parse(currentContext.Contents["ppid"].Value.ToString()),
							"@ExecutionID",SqlDbType.Int,executionId,
							"@Type", SqlDbType.VarChar, workflowType,
							"@SubmitterId", SqlDbType.Int, nSubmitterId));			

				output = "Success";
			}
			catch (Exception ex)
			{				
				logger.LogMessage(ReasonConst.Error, "Unable to create ProtocolProcess Workflow Map in CreateProtocolProcessWorkflowMap.Run() " + ex.ToString());  				
				output = "Failure";										
			}		
			return WorFlowInterface.ActionResult.Completed; 								 					
		}

		/// <summary>
		/// Gets the web control to be displayed in the activity list for this action. No user interface is implemented for
		/// this action. So it returns null.
		/// </summary>
		/// <returns>IActivityWebUIHandler - Interface that allows an object to implement a custom document view Control.</returns>
		public virtual Workflow.NET.Interfaces.IActivityWebUIHandler GetActivityWebUIHandler()
		{
			return null;
		} 

	}
	#endregion
}